package com.github.szhittech.service;


import com.github.szhittech.bean.ext.ClifeArgsExt;
import com.github.szhittech.service.api.ApiService;
import com.github.szhittech.service.api.inter.IDependency;
import com.github.szhittech.service.api.inter.IProjectExecute;
import com.github.szhittech.util.WordUtil;

import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.Dependency;

import java.util.Iterator;

public class ProjectExecuteImpl implements IProjectExecute {
    @Override
    public void execute(Project project) {
        ClifeArgsExt extension = project.getExtensions().getByType(ClifeArgsExt.class);
        project.getConfigurations().all(new Action<Configuration>() {
            @Override
            public void execute(Configuration configuration) {
                String name = configuration.getName();
                if (name == "implementation" || name == "compile" || name == "api") {
                    Iterator<Dependency> it = configuration.getDependencies().iterator();
                    StringBuffer logMgs = new StringBuffer();
                    while (it.hasNext()) {
                        Dependency item = it.next();
                        if (item == null || item.getGroup() == null)
                            continue;
                        /*String lib = item.getGroup() + ":" + item.getName() + ":" + item.getVersion();
                        Logc.i("---- " + lib);*/
                        IDependency dependency = ApiService.getInstance(IDependency.class);
                        if (dependency != null) {
                            String log = dependency.onDependency(extension, item);
                            if (log != null) {
                                logMgs.append(log);
                            }
                        }
                    }
                    if (logMgs.toString() != null && !logMgs.toString().equalsIgnoreCase("")) {
                        printEnd();
                    }
                }

            }
        });
    }

    private void printEnd() {
        StringBuffer sBuffer = new StringBuffer();
        String vrgs =  WordUtil.STRING_15;
        String warm = WordUtil.STRING_16+ vrgs;
        sBuffer.append(warm);
        sBuffer.append("\r\n");
        sBuffer.append("###################################################################");
        System.err.println(sBuffer.toString());
    }
}
